<?php

/**
 * staff actions.
 *
 * @package    erp-dnict
 * @subpackage staff
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class staffActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
  	$q = Doctrine_Query::create()
          ->from('Staff s')
          ->where('s.is_active = ?', true);

    $this->staffs = $q->execute();
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->staff_id = $request->getParameter('id', null);
    if ($this->staff_id) {
      $this->staff = Doctrine_Core::getTable('Staff')->find($this->staff_id);
      $this->staff_profile = $this->staff->Profile;
      $this->pub_organizations = Doctrine_Core::getTable('StaffPubOrganization')->findByStaffId($this->staff->id);
      $this->staff_education_backgrounds = Doctrine_Core::getTable('StaffEducationBackground')->findByStaffId($this->staff_id);

      $url = sfConfig::get('sf_root_dir').DIRECTORY_SEPARATOR."assets".DIRECTORY_SEPARATOR."data".DIRECTORY_SEPARATOR;
      if (!file_exists($url.$this->staff_id.DIRECTORY_SEPARATOR)) {
        $url .= $this->staff_id.DIRECTORY_SEPARATOR;
        mkdir($url, 0777);
        $url .= "files".DIRECTORY_SEPARATOR;
        mkdir($url, 0777);
      } else {
        $url .= $this->staff_id.DIRECTORY_SEPARATOR;
        $url .= "files".DIRECTORY_SEPARATOR;
      }

      $url = "/assets/data/".$this->staff_id."/files/";

      $this->elfinder_connector = "/plugins/elfinder/php/connector.php?path=".$url."url".urlencode(sfConfig::get('sf_root_dir'));
    } else {
      $this->staff = new Staff();
      $this->staff_profile = new StaffProfile();
    }

    $this->form = new StaffForm($this->staff);
    $this->profile_form = new StaffProfileForm($this->staff_profile);
    $this->staff_pub_organization_form = new StaffPubOrganizationForm();
    $this->staff_transfer_form = new StaffTransferForm();
    $this->staff_event_form = new StaffEventForm();
    $this->staff_education_background_form = new StaffEducationBackgroundForm();

    if ($request->isMethod('post')) {
      $this->form->bind($request->getParameter('staff'), $request->getFiles('staff'));
      $this->profile_form->bind($request->getParameter('staff_profile'), $request->getFiles('staff_profile'));
      if ($request->getParameter('id')) { // new creation
        $this->staff = Doctrine_Core::getTable('Staff')->find($request->getParameter('id'));
        $this->staff_profile = $this->staff->Profile;
      } else {
        $this->staff = new Staff();
        $this->staff_profile = new StaffProfile();
      }
      if ($this->form->isValid() && $this->profile_form->isValid()) {
        $this->updateStaffFromData($this->staff, $this->staff_profile, $request->getParameter('staff'), $request->getParameter('staff_profile'), $request->getFiles('staff'));
      }
    }
  }

  private function updateStaffFromData($staff, $staff_profile, $staff_data, $staff_profile_data, $file)
  {
    foreach ($staff_data as $key => $value) {
      if ($key != 'photo') {
        $key = $this->toMethodName($key);
        $staff->{'set'.$key}($value);
      }
    }

    foreach ($staff_profile_data as $key => $value) {
      $key = $this->toMethodName($key);
      $staff_profile->{'set'.$key}($value);
    }

    // update photo
    if ($file['photo']['size'] != 0) {
      $upload_dir = sfConfig::get('app_photo_upload_dir');
      $image = new SfImage($file['photo']['tmp_name'], $file['photo']['type']);
      $image->saveAs($upload_dir.$staff->reference.$staff->name.'.jpeg', $file['photo']['type']);
    }

    $staff_profile->save();
    $staff->profile_id = $staff_profile->id;
    $staff->save();
  }

  private function toMethodName($key) {
    for ($i = 0; $i < strlen($key); $i++) {
        if ($i == 0 || ($i > 0 && $key[$i-1] == '_'))
            $key[$i] = strtoupper($key[$i]);
    }

    $key = str_replace('_', '', $key);
    return $key;
  }

  public function executeAddPubOrganization(sfWebRequest $request)
  {
    $staff_id = $request->getParameter('staff_id');
    $pub_id = $request->getParameter('pub_id');
    $start_date = $request->getParameter('start_date');
    $position = $request->getParameter('position');

    if (!Doctrine_Core::getTable('StaffPubOrganization')->find(array($staff_id, $pub_id))) {
      $staff_pub_organization = new StaffPubOrganization();
      $staff_pub_organization->staff_id = $staff_id;
      $staff_pub_organization->pub_organization_id = $pub_id;
      $staff_pub_organization->start_date = $start_date;
      $staff_pub_organization->position = $position;
      $staff_pub_organization->save();

      $html = '';
      $count = 0;

      $pub_organizations = Doctrine_Core::getTable('StaffPubOrganization')->findByStaffId($staff_id);
      foreach ($pub_organizations as $pub_organization) {
        $count += 1;
        $html .= '<tr><td>'.$count.'</td><td>'.$pub_organization->PubOrganization->name.'</td><td>'.$pub_organization->start_date.'</td><td>'.$pub_organization->position.
        '</td><td><div class="controls center">'.
        "<a href='#' title='Remove task' class='tip'><span onClick='remove_url(".$pub_organization->pub_organization_id.", ".$staff_id.")' class='icon12 icomoon-icon-remove'></span></a></div></td></tr>";
      }
      return $this->renderText($html);
    } else return $this->renderText('error');
  }

  public function executeAddStaffTransfer(sfWebRequest $request)
  {
    $staff_id = $request->getParameter('staff_id');
    $old_gov_organization_id = $request->getParameter('old_gov_organization_id');
    $new_gov_organization_id = $request->getParameter('new_gov_organization_id');
    $start_date = $request->getParameter('start_date');
    $document_id = $request->getParameter('document_id');
    $new_job_title = $request->getParameter('new_job_title');
    $old_job_title = $request->getParameter('old_job_title');

    $staff_transfer = new StaffTransfer();
    $staff_transfer->staff_id = $staff_id;
    $staff_transfer->old_gov_organization_id = $old_gov_organization_id;
    $staff_transfer->new_gov_organization_id = $new_gov_organization_id;
    $staff_transfer->start_date = $start_date;
    $staff_transfer->document_id = $document_id;
    $staff_transfer->new_job_title = $new_job_title;
    $staff_transfer->old_job_title = $old_job_title;

    $staff_transfer->save();

    $staff_transfers = Doctrine_Core::getTable('StaffTransfer')->findByStaffId($staff_id);
    $html = '';
    $count = 0;
    foreach ($staff_transfers as $transfer) {
        $count += 1;
        $html .= '<tr><td>'.$count.'</td><td>'
                          .$transfer->OldGovOrganization->name.'</td><td>'
                          .$transfer->old_job_title.'</td><td>'
                          .$transfer->NewGovOrganization->name.'</td><td>'
                          .$transfer->new_job_title.'</td><td>'
                          .$transfer->start_date.'</td><td>'
                          .$transfer->document_id.'</td><td>'
        .'</td><td><div class="controls center">'.
        "<a href='#' title='Remove task' class='tip'><span onClick='remove_url(".$transfer->id.")' class='icon12 icomoon-icon-remove'></span></a></div></td></tr>";
      }

    return $this->renderText($html);
  }

  public function executeAddStaffEvent(sfWebRequest $request)
  {
    $staff_id = $request->getParameter('staff_id');
    $name = $request->getParameter('name');
    $description = $request->getParameter('description');
    $start_date = $request->getParameter('start_date');
    $document_id = $request->getParameter('document_id');

    $staff_event = new StaffEvent();
    $staff_event->staff_id = $staff_id;
    $staff_event->name = $name;
    $staff_event->description = $description;
    $staff_event->start_date = $start_date;
    $staff_event->document_id = $document_id;

    $staff_event->save();

    $staff_events = Doctrine_Core::getTable('StaffEvent')->findByStaffId($staff_id);
    $html = '';
    $count = 0;
    foreach ($staff_events as $event) {
        $count += 1;
        $html .= '<tr><td>'.$count.'</td><td>'
                          .$event->name.'</td><td>'
                          .$event->description.'</td><td>'
                          .$event->start_date.'</td><td>'
                          .$event->document_id.'</td><td>'
        .'</td><td><div class="controls center">'.
        "<a href='#' title='Remove task' class='tip'><span onClick='remove_url(".$event->id.")' class='icon12 icomoon-icon-remove'></span></a></div></td></tr>";
      }

    return $this->renderText($html);
  }
}
